var tableIns;//表格实例
var style = 'macarons2';
let map = new Map();
layui.use(['laydate', 'form'], function() {
	var form = layui.form;
	io.get("/ep/data/comm_query_industry_list_l3", function(res) {
		for (let v of res.data) {
			map.set(v.label, v.label);
		}
		map2Select(map, 'industry');
		form.render();
	});
	var laydate = layui.laydate;
	let today = new Date();
	let twoYearsAgo = new Date();
	twoYearsAgo.setFullYear(today.getFullYear() - 5);
	laydate.render({
		elem: '#startDate'
		, type: 'date'
		, format: 'yyyyMMdd'
		, value: twoYearsAgo
	});
	laydate.render({
		elem: '#endDate'
		, type: 'date'
		, format: 'yyyyMMdd'
		, value: today
	});

	form.on('submit(search)', function(formData) {
		render(formData.field);
	});

});

function render(params) {
	$('#title').html(map.get(params.industry));
	//交易指标
	params.sqlid="bi_industry_indicators_single_sum";
	_render(params,"d_stock_mv","total_mv","mv","总市值",10000);
	params.sqlid="bi_industry_indicators_single";
	_render(params,"d_stock_mv","pe_ttm","pettm","PE-TTM");
	_render(params,"d_stock_mv","close","close","股票价格");
	_render(params,"d_stock_mv","pb","pb","市净率");
	_render(params,"d_stock_mv","dv_ttm","dvttm","股息率TTM(%)");
	_render(params,"d_stock_mv","ps_ttm","psttm","市销率TTM(%)");
	//收入指标
	params.tableName="d_stock_income";
	params.indicatorField="total_revenue";
	params.unit=10000*10000;
	io.get("/stock/single/indicators", function(res) {
		params.tableName="d_stock_income";
		params.indicatorField="total_cogs";	
		io.get("/stock/single/indicators", function(res2) {
			renderLine2("total_revenue","收入/成本",["总收入","总成本"],res.attr.category,[res.attr.data,res2.attr.data]);
		}, params);
	}, params);
	params.tableName="d_stock_fina_indicator";
	params.indicatorField="gross_margin";
	io.get("/stock/single/indicators", function(res) {
		params.tableName="d_stock_income";
		params.indicatorField="n_income_attr_p";	
		io.get("/stock/single/indicators", function(res2) {
			params.tableName="d_stock_fina_indicator";
			params.indicatorField="profit_dedt";	
			io.get("/stock/single/indicators", function(res3) {
				renderLine2("gross_margin","利润",["毛利润","净利润","净利润（扣非）"],res.attr.category,[res.attr.data,res2.attr.data,res3.attr.data]);
			}, params);
		}, params);
	}, params);
	params.tableName="d_stock_income";
	params.indicatorField="sell_exp";
	io.get("/stock/single/indicators", function(res) {
		params.tableName="d_stock_income";
		params.indicatorField="admin_exp";	
		io.get("/stock/single/indicators", function(res2) {
			params.tableName="d_stock_income";
			params.indicatorField="fin_exp";	
			io.get("/stock/single/indicators", function(res3) {
				params.tableName="d_stock_income";
				params.indicatorField="oper_cost";	
				io.get("/stock/single/indicators", function(res4) {
						renderLine2("fees","费用",["销售费用","管理费用","财务费用","营业成本"],res.attr.category,[res.attr.data,res2.attr.data,res3.attr.data,res4.attr.data]);
				}, params);
			}, params);
		}, params);
	}, params);
	_render(params,"d_stock_income","assets_impair_loss","assets_impair_loss","资产减值损失",10000*10000);
	_render(params,"d_stock_fina_indicator","extra_item","extra_item","非经常性损益",10000*10000);
	
	//财务指标
	_render(params,"d_stock_fina_indicator","eps","eps","基本每股收益");
	_render(params,"d_stock_fina_indicator","saleexp_to_gr","saleexp_to_gr","销售费用/营业总收入");
	_render(params,"d_stock_fina_indicator","fcff","fcff","企业自由现金流量",10000*10000);
	_render(params,"d_stock_fina_indicator","roe","roe","净资产收益率");
	_render(params,"d_stock_fina_indicator","roic","roic","投入资本回报率");
	_render(params,"d_stock_fina_indicator","debt_to_assets","debt_to_assets","资产负债率");
	_render(params,"d_stock_fina_indicator","q_profit_yoy","q_profit_yoy","净利润同比增长率(%)(单季度)");
	_render(params,"d_stock_fina_indicator","rd_exp","rd_exp","研发费用",10000*10000);
	//资产负债
	params.tableName="d_stock_balancesheet";
	params.indicatorField="total_assets";
	params.unit=10000*10000;
	io.get("/stock/single/indicators", function(res) {
		params.tableName="d_stock_balancesheet";
		params.indicatorField="total_liab";	
		io.get("/stock/single/indicators", function(res2) {
			renderLine2("total_assets","资产负债",["资产","负债"],res.attr.category,[res.attr.data,res2.attr.data]);
		}, params);
	}, params);
	_render(params,"d_stock_balancesheet","inventories","inventories","存货",10000*10000);
	_render(params,"d_stock_balancesheet","goodwill","goodwill","商誉",10000*10000);
	//现金流量
	_render(params,"d_stock_cashflow","n_cash_flows_fnc_act","n_cash_flows_fnc_act","筹资活动产生的现金流量净额",10000*10000);
	_render(params,"d_stock_cashflow","n_incr_cash_cash_equ","n_incr_cash_cash_equ","现金及现金等价物净增加额",10000*10000);
}

function _render(params,table,field,elementId,title,unit){
	var _params={...params};
	_params.tableName=table;
	_params.indicatorField=field;
	if(unit){
		_params.unit=unit;
	}else{
		_params.unit=1;
	}
	io.get("/stock/single/indicators", function(res) {
		renderLine(elementId, res,title);
	}, _params);
}

function renderLine(elementId, data,title) {
	var line = echarts.init(document.getElementById(elementId), style);
	option = {
		title: {
			text: title,
			left: 'center'
		},
		tooltip: {
		    trigger: 'axis',
		    axisPointer: {
		      type: 'cross'
		    }
		},
		xAxis: {
			type: 'category',
			data: data.attr.category
		},
		yAxis: {
			type: 'value'
		},
		series: [
			{
				data: data.attr.data,
				type: 'line',
      			stack: 'Total',
				label: {
			        show: true,
			        position: 'top'
			      },
		      	areaStyle: {},
		      	emphasis: {
		        	focus: 'series'
		      	},
			}
		]
	};
	line.setOption(option);
}

function renderLine2(elementId,title,legend,category,data){
	var line = echarts.init(document.getElementById(elementId), style);
	option = {
		title: {
			text: title
		},
		tooltip: {
		    trigger: 'axis',
		    axisPointer: {
		      type: 'cross'
		    }
		},
		legend: {
		},
		xAxis: {
			type: 'category',
			boundaryGap: false,
			data: category
		},
		yAxis: {
			type: 'value'
		},
	};
	var series = [];
	for (var i = 0; i < data.length; i++) {  
	 series[i]={
				name: legend[i],
				data: data[i],
				type: 'line',
				label: {
			        show: true,
			        position: 'top'
			      },
			};
	}
	option.series=series;
	line.setOption(option);
}